<?php
namespace Home\Controller;
use Think\Controller;
class ArticleController extends Controller {
    /**
     * 资讯列表
     */
    public function showlist() {
        $M_article = M('article');    
        $count = $M_article->count();  //查出总数
        $Page = new \Think\Page($count, 10);  // 实例化 分页类
        //定制分页类
        $Page->setConfig('header','<span class="sabrosus">共 %TOTAL_ROW% 条记录</span>'); //显示内容
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('first','首页');
        $Page->setConfig('last','末页');
        $Page->setConfig('theme'," %HEADER% %FIRST% %UP_PAGE%  %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $Page -> show();    
        $article_list = $M_article->limit($Page->firstRow.','.$Page->listRows)
                                    ->order('addtime DESC')    
                                    ->select();
        $this->article_list = $article_list;
        $this->page = $show;
        $this->display();
    }
    
    /**
     * 添加资讯
     */
    public function add() {
        if(IS_POST) {
            $M_article = M('article');
            
            $data = I("post.");
            $data['content'] = htmlspecialchars_decode(I('post.content'));
            $data['addtime'] = date("Y-m-d H:i:s");
            
            $added = $M_article->add($data);
            
            if ($added){
                $this->success("添加成功", U("showlist"));
            } else {
                $this->error("添加失败");
            }
        }  else {
            $M_article = M('article_sort');
            $cate_row = $M_article->select();
            $this->cate_row = $cate_row;
            $this->display();
        }
    }
    
    /**
     * 修改资讯
     */
    public function mod() {
        $M_article = M('article');
        
        if(IS_POST){
            $data   = I("post.");
            $data['content'] = htmlspecialchars_decode(I('post.content'));
            $data['addtime'] = date("Y-m-d H:i:s");
            //print_r($data);die;
            $moded  = $M_article->where("id = $data[id]")->save($data);
            if($moded){
                $this -> success("修改成功", U("showlist"));
            }else{
                $this -> error("修改失败");
            }
        } else {	
            $id     = I('get.id');
            $data   = $M_article->where(array("id"=>$id))->find();
            $M_article = M('article_sort');
            $cate_row = $M_article->select();
            $this->cate_row = $cate_row;
            $this->data = $data;
            $this->display();
        }
    }
    
    /**
     * 删除资讯
     */
    public function del() {
        $M_article  = M("article");
        $id         = I("get.id");
        $del_row    = $M_article->where(array('id'=>$id))->delete();
        if ($del_row) {
            $this->success("删除成功", U("showlist"));
        } else {
            $this->error("删除失败");
        }
    }
    /**
     * 资讯分类列表
     */
    public function sortlist() {
        $M_category = M('article_sort');
        $category_rows = $M_category->select();
        $this->category_rows = $category_rows;
        $this->display();
    }
    /**
     * 添加分类
     */
    public function sortadd() {
        if(IS_POST){
            $M_category = M('article_sort');
            $data['title'] = I('post.title');
            $data['addtime'] = date("Y-m-d H:i:s");
            $added_row = $M_category->add($data);
            if ($added_row) {
                $this->success('添加成功', U('sortlist'));
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->display();
        }
    }
    /**
     * 删除分类
     */
    public function sortdel($id) {
        $id = intval($id);
        $M_category = M('article_sort');

        $cate_row = $M_category->find($id);
        if (!$cate_row) {
            $this->error('该分类不存在！');
        }

        $deled_cate = $M_category->delete($id);
        if ($deled_cate) {
            $this->success('分类删除成功！');
        } else {
            $this->error('分类删除失败！');
        }
    }
    /**
     * 修改分类
     */
    public function sortmod() {
        $M_category = M('article_sort');
        if (IS_POST) {
            $id = I('post.id','','intval');
            $cate_row = $M_category->find($id);
            if (!$cate_row) {
                $this->error('该分类不存在！');
            }
            $cate_row['title'] = I('post.title');
            $updated_row = $M_category->save($cate_row);
            if ($updated_row){
                $this->success('修改成功', U('sortlist'));
            } else {
                $this->error('修改失败');
            }
        } else {
            $id = I('get.id','','intval');
            $cate_row = $M_category->find($id);
            $this->cate_row = $cate_row;
            $this->display();
        }
    }
}